package com.yunxi.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.order.pojo.*;
import com.yunxi.order.service.VehicleReturApplyDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wholesale/vehicleReturApply")
@Api(tags = "wholesale:vehicleReturApply", description = "渠道商申请退车接口")
public class VehicleReturApplyController {

    @Autowired
    VehicleReturApplyDubboService vehicleReturApplyDubboServiceImpl;

    @ApiOperation(value = "渠道商退车申请主数据查询")
    @GetMapping(value = "/page")
    public ApiResult<VehicleReturApplyQueryRespVO> page(@CurrentUser LoginInfo user,VehicleReturApplyQueryReqVO vo, Integer pageNum, Integer pageSize){

        Object partnerVO = (Object) user.getPartner();
        if(partnerVO == null){
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
        PageInfo<VehicleReturApplyQueryRespVO> pageInfo = vehicleReturApplyDubboServiceImpl.queryByList(vo,pageNum,pageSize);
        return ResponseUtil.ok(pageInfo);
    }

    @ApiOperation(value = "渠道商退车申请")
    @PostMapping(value = "/applyReturVehicle")
    public ApiResult<Boolean> applyReturVehicle(@CurrentUser LoginInfo user,@RequestBody VehicleReturApplyReqVO vo){

        Object partnerVO = (Object) user.getPartner();
        if(partnerVO == null){
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
        vo.setUserCode(user.getUserCode());
        boolean isSuccess = vehicleReturApplyDubboServiceImpl.applyReturVehicle(vo);
        return ResponseUtil.ok(isSuccess);
    }

    @ApiOperation(value = "渠道商退车出库")
    @PostMapping(value = "/outbound")
    public ApiResult<Boolean> outbound(@CurrentUser LoginInfo user,@RequestBody VehicleReturOutboundReqVO vo){
        Object partnerVO = (Object) user.getPartner();
        if(partnerVO != null){
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO),PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        }else{
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
        boolean isSuccess = vehicleReturApplyDubboServiceImpl.outbound(vo);
        return ResponseUtil.ok(isSuccess);
    }

    @ApiOperation(value = "整车退车出库数据查询")
    @GetMapping(value = "/queryVehicleReturOut")
    public ApiResult<VehicleReturApplyQueryRespVO> queryVehicleReturOut(@CurrentUser LoginInfo user,VehicleReturOutReqVO vo, Integer pageNum, Integer pageSize){

        Object partnerVO = (Object) user.getPartner();
        if(partnerVO == null){
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401,"当前用户不是渠道商用户，请使用渠道商账户登录！");
        }
        PageInfo<VehicleReturOutRespVO> pageInfo = vehicleReturApplyDubboServiceImpl.queryVehicleReturOut(vo,pageNum,pageSize);
        return ResponseUtil.ok(pageInfo);
    }

}
